package com.huixue.cn.ztej.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.huixue.cn.ztej.bean.LoginLogger;
import com.huixue.cn.ztej.bean.Results;
import com.huixue.cn.ztej.service.SysLoginLoggerService;
import com.huixue.cn.ztej.service.SysUserService;
import com.huixue.cn.ztej.utils.RemoteUtils;
import com.huixue.cn.ztej.utils.SystemContents.API_RETURN_STATUS;
import com.huixue.cn.ztej.utils.annotation.RequestMappingName;

@Controller
@RequestMapping(value = "/sys")
public class LoginController extends BaseController {
	private static Logger logger = LoggerFactory
			.getLogger(LoginController.class);
	
	@Autowired(required = true)
	SysUserService sysUserService;

	
	@Autowired(required = true)
	SysLoginLoggerService sysLoginLoggerService;
	/**
	 * 跳转到登陆页面
	 * 
	 * @return
	 */
	@RequestMappingName(value = "跳转到登陆页面")
	@RequestMapping(value = "/toLogin", method = RequestMethod.GET)
	public String toLogin() {
		return "/login/login";
	}

	@RequestMappingName(value = "跳转到系统首页")
	@RequestMapping(value = "/toIndex", method = RequestMethod.GET)
	public String toIndex() { 
		return "/admin/index";
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@RequestMappingName(value = "用户登录登录")
	@ResponseBody
	public Results login(HttpServletRequest request) {
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(),
				API_RETURN_STATUS.NORMAL.desc());

		// 此处默认有值
		String username = request.getParameter("userName");
		// MD5加密
		String password = request.getParameter("password");
		// 获取IP
		String ip = RemoteUtils.getRemoteHost(request);

		results = sysUserService.userLogin(request, username, password, ip);
		return results;
	}
	
	/**
	 * 跳转到登陆页面
	 * @return
	 */
	@RequestMappingName(value = "退出登录,然后跳转到登录页面")
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String toLogout(HttpServletRequest request) {
    	String result = "/login/login";
    	try{
    		
    		//保存退出信息
    		String loginName = (String)request.getSession().getAttribute("loginAccount");
    		String userName = (String)request.getSession().getAttribute("loginUserName");
    		
    		// 获取IP
    		String ip = RemoteUtils.getRemoteHost(request);
    		LoginLogger logger = new LoginLogger(loginName,userName,ip,new Date(),2);	
			
    		sysLoginLoggerService.addLogger(logger);
			
    		request.getSession().removeAttribute("loginAccount");
    		request.getSession().removeAttribute("loginTime");
    		
    	}catch(Exception e){
    		logger.error("用户注销登录异常："+e.getMessage());
    	}
		return result;
		
	}

}
